package com.qc31.monitorlib.ui.carmonitor

import autodispose2.autoDispose
import com.qc31.comlib.TimeUtil
import com.qc31.comlib.entity.HistoryLine
import com.qc31.comlib.net.ServiceHelper
import com.qc31.monitorlib.R
import com.qc31.monitorlib.common.ChooseTimeViewModel
import com.qc31.monitorlib.uitls.DataUtil
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.functions.Consumer
import io.reactivex.rxjava3.subjects.PublishSubject

/**
 *@Author Jasper
 *@Time   2020/8/8
 *@Desc   選擇车辆
 */
class HistoryLineViewModel : ChooseTimeViewModel() {
    var carId = ""
    var carName = ""
    //上传的参数
    var params=""

    private val historyLineSub: PublishSubject<HistoryLine> = PublishSubject.create()
    fun historyLineObserver(): Observable<HistoryLine> = historyLineSub.hide()

    fun getHistory(
        startTime: String,
        endTime: String,
        filter: Boolean,
        maxSpeed: String,
        stopLong: String,
        filterInvalid: Boolean,
        filterAppend: Boolean
    ) {
        if (carId.isEmpty()) {
            toast(R.string.hint_toast_choose_car)
            return
        }
        val speed: String =
            if (maxSpeed.isEmpty()) {
                "0"
            } else {
                maxSpeed
            }
        val long = if (stopLong.isEmpty()) {
            "0"
        } else {
            stopLong
        }
        if (isTimeOk(startTime, endTime)) return
        val start=  TimeUtil.instance.formatHms(startTime)
        val end=  TimeUtil.instance.formatHms(endTime)
        toastLoading(true)
        params="$carId,$start,$end,$filter,$filterInvalid,$filterAppend,$startTime,$endTime"
        ServiceHelper.getRepository().getAllHisGps(
            carId,
            start,
            end,
            speed,
            long,
            filterInvalid,
            filterAppend,
            filter
        ).autoDispose(this).subscribe(Consumer {
            toastLoading(false)
            if (isHasListData(it.locations, R.string.toast_no_history_line)) return@Consumer
            historyLineSub.onNext(it)
            DataUtil.HistoryLine = it
        }, error)
    }

}
